KISSY.use(["node",'bui/form'],function(S,Node,Form){ 
	var form = new Form.Form({
        srcNode : '#eventform'
      }).render();
	  form.valid();
	$(".start-calendar").on("click",function(){
		var start_time = form.getField('start_time');
		start_time.set('remote',{
			url : '/?m=shopmanage&c=eventset&a=check_time&eid='+$(".eventid").val()+"&end_time="+$(".end-calendar").val()+"&tp=start-calendar",
			dataType:'json',//默认为字符串
			callback : function(data){
				if(data.success){
					return '';
				}else{
					return data.msg;
				}
			},
			"complete":function(data){
				$(".set_time").on("click",function(){
					$("."+data.where).val(data.time);
					form.valid();
				});
			},
		});
	});
	$(".end-calendar").on("click",function(){
		var end_time = form.getField('end_time');
		end_time.set('remote',{
			"url" : '/?m=shopmanage&c=eventset&a=check_time&eid='+$(".eventid").val()+"&start_time="+$(".start-calendar").val()+"&tp=end-calendar",
			"dataType":'json',//默认为字符串
			"callback" : function(data){
				if(data.success){
					return '';
				}else{
					return data.msg;
				}
			},
			"complete":function(data){
				$(".set_time").on("click",function(){
					$("."+data.where).val(data.time);
					form.valid();
				});
			},
			"beforeSend":function(){
				// end_time.set('remote',start_time_str);
			}
		});
	});
	$(".onoff").on("change",function(){
		var d = $(this).attr("data-input");
		if($(this).val()==1){
			$("."+d).removeAttr("readonly");
			$("."+d).css({"background":"#FFF"});
		}else{
			$("."+d).attr("readonly",true);
			$("."+d).css({"background":"#DDD"});
		}
	});
	$(".brandme").on("click",function(){
		if($(this).getDOMNode().checked==true){
			$(".brandid").val($(".brandid").attr("data-val"));
			$(".brandid").removeAttr("readonly");
			$(".brandid").css("backgroundColor","#FFF");
		}else{
			$(".brandid").val(0);
			$(".brandid").attr("readonly","readonly");
			$(".brandid").css("backgroundColor","#DDD");
		}
	});
	$(".helper").on("click",function(){
		showDialog("帮助",$(this).attr("data-tip"),"form-horizontal");
		$(".helper-in").on("click",function(){
			showDialog("帮助",$(this).attr("data-tip"),"form-horizontal");
		});
	});
	function showDialog(title,msg){
		BUI.Message.Alert('<h3>'+title+'</h3>'+msg,function(){},'info');
	}
	function closeDialog(){
		$(".button-primary").fire("click");
	}
	$(".js-itemset").on("click",function(){
		getitems("","","","order_items");
	});
	$(".js-cartset").on("click",function(){
		getitems("","","","cart_items");
	});
	function getitems(q,pg,numiids,input){
		Q=q?q:"";
		PG=pg?pg:1;
		input=input?input:"order_items";
		showDialog("正在读取数据。。。","<img src=\"http://gtd.alicdn.com/tps/i3/T1b1m3XkVpXXXXXXXX-32-32.gif\" />","form-horizontal");	
		var item_iids = $('.'+input).val();
		if(item_iids!=""){
			num_iids = JSON.parse(decodeURI(item_iids));
		// num_iids = item_iids.split(",");
		}else{
		num_iids=new Array();
		}
		KISSY.io({
			url: "/?a=items&m=shopmanage&c=Service",
			data:{"q":Q,"pg":PG,"numiids":numiids},
			success: function(data, textStatus) {
			closeDialog();
			if(data.code){
				showDialog("读取失败！",data.msg+data.sub_msg,"form-horizontal");
				return;
			}
			if(data.total_results==0){
				showDialog("读取失败！","没有读取到任何商品哦！","form-horizontal");
			}else{
				var item="<input type=\"text\" value=\""+data.q+"\" class=\"js-gjc\" /><button type=\"button\" class=\"button button-mini js-so\">搜索</button>  共"+data.total_results+"件商品  <button type=\"button\" class=\"button button-mini button-success js-ready\" data-input=\""+input+"\">查看已设置的商品</button> <button type=\"button\" class=\"button button-mini js-ready-all\">查看全部</button> <button type=\"button\" class=\"button button-mini js-select-all\">全选本页</button>";
				item +=("<hr style=\"clear:both\">");
				for(var i=0;i<data.items.item.length;i++){
				item +=("<table style=\"width:140px;float:left;margin-right:5px\" cellspacing=\"0\" class=\"table table-bordered table-item\">");
				item +=("	<tbody>");
				item +=("	<tr><td>");
				item +=("		<a href=\"http://item.taobao.com/item.htm?id="+data.items.item[i].num_iid+"\" target=\"_blank\" ><img src=\""+data.items.item[i].pic_url+"_120x120.jpg\"></a>");
				// item +=("	</td></tr><tr><td>");
				item +=("	商品标题："+decodeURI(data.items.item[i].title));
				var css = "";
				var text = "选择";
				for (var j=0;j<num_iids.length;j++){
					if(data.items.item[i].num_iid==num_iids[j][0]){
						css = "button-danger";
						text = "取消选择";
						break;
					}
				}
				item +=("	<div style=\"text-align:center\"><button type=\"button\" class=\"button button-small "+css+" js-select\" data-price=\""+data.items.item[i].price+"\" data-title=\""+decodeURI(data.items.item[i].title)+"\" data-pic=\""+data.items.item[i].pic_url+"\" data-iid=\""+data.items.item[i].num_iid+"\">"+text+"</button></div>");
				item +=("	</td></tr>");
				item +=("	</tbody>");
				item +=("</table>");
				}
				item +=("<hr style=\"clear:both\">");
				item +=("<div class=\"centered\"><button type=\"button\" class=\"button button-mini js-pgup\">上一页</button>");
				item +=("<button type=\"button\" class=\"button button-mini js-pgdown\">下一页</button></div>");
				showDialog("请选择商品",item,"form-horizontal",KISSY.DOM.scrollTop());
				$(".js-ready-all").on("click",function(){
					getitems("","","",$(this).attr("data-input"));
				});
				$(".js-select-all").on("click",function(){
					$(".js-select").fire("click");
				});
				$(".js-select").on("click",function(){
					var key = $(this).attr("data-iid");
					var pic = $(this).attr("data-pic");
					var title = $(this).attr("data-title");
					var price = $(this).attr("data-price");
					var item_json = new Array(key,pic,title,price);
					if($(this).hasClass("button-danger")){
						for(var i=0;i<num_iids.length;i++){
							if(num_iids[i][0]==key){
								num_iids.splice(i,1);
							}
						}
						$("."+input).val(encodeURI(JSON.stringify(num_iids)));
						// $("."+input).val(num_iids.join(","));
						$(this).removeClass("button-danger");
						$(this).text("选择");
						$(".items-"+input).text(num_iids.length);
					}else{
						num_iids.push(item_json);
						$("."+input).val(encodeURI(JSON.stringify(num_iids)));
						// num_iids.push(key);
						// $("."+input).val(num_iids.join(","));
						$(this).addClass("button-danger");
						$(this).text("取消选择");
						$(".items-"+input).text(num_iids.length);
					}
				});
				$(".js-pgup").on("click",function(){
					if(PG==1){
						alert("没有上一页啦！");
					}else{
						PG--;
						getitems($(".js-gjc").val(),PG,numiids,input);
					}
				});
				$(".js-pgdown").on("click",function(){
					if(PG>(data.total_results/20)){
						alert("没有下一页啦！");
					}else{
						PG++
						getitems($(".js-gjc").val(),PG,numiids,input);
					}
				});
				$(".js-ready").on("click",function(){
					var item_iids = $('.'+input).val();
					if(!item_iids){
					alert("您还没有选择任何商品！");
					}else{
					getitems("","",item_iids,input);
					}
				});
			}
			$(".js-so").on("click",function(){
				getitems($(".js-gjc").val(),"","",input);
			});
			},
			complete: function(data){
				
			},
			error: function(){
				alert('error');
			},
			dataType:"json"
		});
}
});